{include file="public/layout" /}
<body style="background-color: #FFF; overflow: auto;">
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page" style="box-shadow:none;">
    <div class="fixed-bar">
        <div class="item-title"><a class="back_xin" href="javascript:history.back();" title="返回"><i class="iconfont e-fanhui"></i></a>
            <div class="subject">
                <h3>编辑参数</h3>
                <h5></h5>
            </div>
        </div>
    </div>
    <form class="form-horizontal" id="post_form" action="{:U('Product/attribute_edit')}" method="post">
        <div class="ncap-form-default">
            <dl class="row">
                <dt class="tit">
                    <label for="typeid"><em>*</em>所属栏目</label>
                </dt>
                <dd class="opt"> 
                    <select name="typeid" id="typeid">
                        {$arctype_html}
                    </select>
                    <span class="err" id="err_typeid" style="color:#F00; display:none;"></span>
                    <p class="notic"></p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label for="ac_name"><em>*</em>参数名称</label>
                </dt>
                <dd class="opt">
                    <input type="text" name="attr_name" value="{$field.attr_name}" id="attr_name" class="input-txt">
                    <span class="err" id="err_attr_name" style="color:#F00; display:none;"></span>
                    <p class="notic"></p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label for="attr_input_type"><em>*</em>表单类型</label>
                </dt>
                <dd class="opt">
                    <label><input type="radio" value="0" name="attr_input_type" {empty name="field.attr_input_type"}checked="checked"{/empty} />单行文本</label>
                    <label><input type="radio" value="2" name="attr_input_type" {eq name="field.attr_input_type" value="2"}checked="checked"{/eq} />多行文本</label>
                    <label><input type="radio" value="1" name="attr_input_type" {eq name="field.attr_input_type" value="1"}checked="checked"{/eq} />下拉框</label>
                    <span class="err" id="err_attr_input_type" style="color:#F00; display:none;"></span>
                    <p class="notic"></p>
                </dd>
            </dl>
            <dl class="row attr_input_type2 {neq name='field.attr_input_type' value='1'}none{/neq}">
                <dt class="tit">
                    <label for="attr_values"><em>*</em>可选值列表</label>
                </dt>
                <dd class="opt">
                    <textarea rows="10" cols="30" name="attr_values" id="attr_values" class="input-txt" style="height:100px;" placeholder="一行代表一个可选值">{$field.attr_values}</textarea>
                    <span id="err_attr_values" class="err" style="color:#F00; display:none;"></span>
                    <p class="notic">一行代表一个可选值</p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label>排序</label>
                </dt>
                <dd class="opt">    
                    <input type="text" value="{$field.sort_order}" name="sort_order" id="sort_order" class="input-txt">
                    <span class="err"></span>
                    <p class="notic">越小越靠前</p>
                </dd>
            </dl>
            <div class="bot">
                <input type="hidden" name="attr_id" value="{$field.attr_id}">
                <a href="JavaScript:void(0);" onclick="check_submit('post_form');"  class="ncap-btn-big ncap-btn-green" id="submitBtn">确认提交</a>
        </div>
    </form>
</div>
<script type="text/javascript">
    var parentObj = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    $(function(){
        $('input[name=attr_input_type]').click(function(){
            var val = $(this).val();
            if (val == 1) {
                $('.attr_input_type2').show();
            } else {
                $('.attr_input_type2').hide();
            }
        });

        var dftypeid = {$field.typeid|default='0'};
        $('#typeid').change(function(){
            var current_channel = $(this).find('option:selected').data('current_channel');
            if (0 < $(this).val() && {$channeltype} != current_channel) {
                showErrorMsg('请选择对应模型的栏目！');
                $(this).val(dftypeid);
            } else if ({$channeltype} == current_channel) {
                layer.closeAll();
            }
        });
    });

    /**
    * ajax 提交表单 到后台去验证然后回到前台提示错误
    * 验证通过后,再通过 form 自动提交
    */
    function check_submit(form_id)
    {
        if ($('#typeid').val() == 0) {
            showErrorMsg('请选择栏目…！');
            $('#typeid').focus();
            return false;
        }
        if($.trim($('input[name=attr_name]').val()) == ''){
            showErrorMsg('参数名称不能为空！');
            $('input[name=attr_name]').focus();
            return false;
        }
        if ($('input[name=attr_input_type]').is(':checked') == false) {
            showErrorMsg('请选择表单类型！');
            $($('input[name=attr_input_type]')[0]).focus();
            return false;
        }
        if ($('input[name=attr_input_type]:checked').val() == 1 && $.trim($('#attr_values').val()) == '') {
            showErrorMsg('可选值列表不能为空！');
            $('#attr_values').focus();
            return false;
        }

        layer_loading('正在处理');
        $.ajax({
            type : "POST",
            url  : "{:url('Product/attribute_edit', ['_ajax'=>1])}",
            data : $('#'+form_id).serialize(),// 你的formid
            dataType: "JSON",
            success: function(res) {
                layer.closeAll();
                if(res.status == 1)
                {
                    var _parent = parent;
                    _parent.layer.close(parentObj);    
                    if(res.hasOwnProperty('data')){
                        if(res.data.hasOwnProperty('url')){
                            _parent.layer.msg(res.msg, {shade: layer_shade, time: 1000}, function(){
                                _parent.gourl(res.data.url);
                            });
                        }else{
                            _parent.layer.msg(res.msg, {shade: layer_shade, time: 1000}, function(){
                                _parent.window.location.reload();
                            });
                        }
                    }else{
                        _parent.layer.msg(res.msg, {shade: layer_shade, time: 1000}, function(){
                            _parent.window.location.reload();
                        });
                    }
                    return true;
                } else {     
                    showErrorMsg(res.msg);                       
                    return false;
                }
            },
            error: function(e) {
                layer.closeAll();
                showErrorAlert(e.responseText);
                return false;
            }
        });   
    }
</script>

{include file="public/footer" /}